//
//  UIButton+UIButtonImageWithLabel.m
//  ylwj_User
//
//  Created by wj on 16/5/23.
//  Copyright © 2016年 邹庆林. All rights reserved.
//

#import "UIButton+UIButtonImageWithLabel.h"

@implementation UIButton (UIButtonImageWithLabel)

- (void) setImage:(UIImage *)image withTitle:(NSString *)title forState:(UIControlState)stateType {
    //UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)
    
    CGSize titleSize = [title sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
    
    [self.imageView setContentMode:UIViewContentModeCenter];
    CGFloat imageXEdg = 0.5 * titleSize.width;
    
    CGFloat titleXEdg = 0.5 * image.size.width;
    
    CGFloat imageYEdg = 0.5 * titleSize.height;
    
    CGFloat titleYEdg = 0.5 * image.size.height;
    
    [self setImageEdgeInsets:UIEdgeInsetsMake(-imageYEdg,
                                              imageXEdg,
                                              imageYEdg,
                                              -imageXEdg)];
    
    [self setImage:image forState:stateType];
    [self.titleLabel setContentMode:UIViewContentModeCenter];
    [self.titleLabel setBackgroundColor:[UIColor clearColor]];
    [self.titleLabel setFont:[UIFont systemFontOfSize:12]];
    [self setTitleColor:CSWhiteColor forState:UIControlStateNormal];
    [self setTitleColor:CSBlackColor forState:UIControlStateSelected];
    [self setTitleEdgeInsets:UIEdgeInsetsMake(titleYEdg,
                                              -titleXEdg,
                                              -titleYEdg,
                                              titleXEdg)];
    
    [self setTitle:title forState:stateType];
    
}

- (void) setImage:(UIImage *)image withTitle:(NSString *)title withWith:(CGFloat) btnWith forState:(UIControlState)stateType {
    //UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)
    
    CGSize titleSize = [title sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
    
    [self.imageView setContentMode:UIViewContentModeCenter];
    CGFloat imageEdg = btnWith - image.size.width;
    NSLog(@"self.width = %f,imageWith = %f",btnWith,image.size.width);
    if (imageEdg < 0) {
        
        imageEdg = imageEdg * -1;
    }
    [self setImageEdgeInsets:UIEdgeInsetsMake(-8.0,
                                              imageEdg / 2.0,
                                              0.0,
                                              0.0)];
    [self setImage:image forState:stateType];
    NSLog(@"titlewith = %f,",titleSize.width);
    NSLog(@"imagewith = %f,",image.size.width);
    [self.titleLabel setContentMode:UIViewContentModeCenter];
    [self.titleLabel setBackgroundColor:[UIColor clearColor]];
    [self.titleLabel setFont:[UIFont systemFontOfSize:12]];
    [self.titleLabel setTextColor:[UIColor whiteColor]];
    [self setTitleEdgeInsets:UIEdgeInsetsMake(image.size.height + 30,
                                              -image.size.width,
                                              0.0,
                                              0.0)];
    [self setTitle:title forState:stateType];
}

@end
